package com.flaremars.picker.adapters;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.flaremars.picker.R;
import com.flaremars.picker.adapters.common.BaseRecyclerAdapter;
import com.flaremars.picker.entities.PostItem;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * @author FlareMars on 2016/3/12
 * 论坛界面 RecylerView Adapter
 */
public class PostItemAdapter extends BaseRecyclerAdapter<PostItem> {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);

    private Context context;

    public PostItemAdapter(final Context context,ViewGroup parentView,List<PostItem> objects) {
        this.context = context;
        setDatas(objects);
        setupHeaderView(parentView);
        setOnItemClickListener(new OnItemClickListener<PostItem>() {
            @Override
            public void onItemClick(int position, PostItem data) {
                Toast.makeText(context, data.getUsername(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setupHeaderView(ViewGroup parentView) {
        View headerView = LayoutInflater.from(context).inflate(R.layout.view_bbs_fragment_header,parentView,false);
        setHeaderView(headerView);
    }


    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {

        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_post, parent, false);
        return new MyHolder(layout);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, final PostItem data) {

        if(viewHolder instanceof MyHolder) {
            final MyHolder holder =(MyHolder) viewHolder;
            holder.usernameTv.setText(data.getUsername());
            holder.toCommentsPanelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, data.getContent(), Toast.LENGTH_SHORT).show();
                }
            });
            holder.contentTv.setText(data.getContent());
            holder.timeTv.setText(DATE_FORMAT.format(data.getTime()));
            if (RealPosition == getItemCount() - 2) { //隐藏最后一个item的分割线，因为存在headerView，所以实际位置 -2
                holder.dividerView.setVisibility(View.INVISIBLE);
            } else {
                holder.dividerView.setVisibility(View.VISIBLE);
            }
            holder.picturesRecyclerView.setAdapter(buildAdapter(data.getPictures()));
        }
    }

    private RecyclerView.Adapter buildAdapter(List<String> pictures) {
        return new SingleImageAdapter(context,pictures);
    }

    class SingleImageAdapter extends RecyclerView.Adapter<SingleImageAdapter.ViewHolder> {

        private Context context;
        private List<String> pictureUrls;

        public SingleImageAdapter(Context context, List<String> pictureUrls) {
            this.context = context;
            this.pictureUrls = pictureUrls;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_view_single_imageview,parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Picasso.with(context).load(pictureUrls.get(position)).placeholder(R.mipmap.ic_launcher).into(holder.image);
        }

        @Override
        public int getItemCount() {
            return pictureUrls.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            private ImageView image;

            public ViewHolder(View itemView) {
                super(itemView);
                image = (ImageView) itemView.findViewById(R.id.image);
            }
        }
    }


    class MyHolder extends BaseRecyclerAdapter.Holder {

        private TextView usernameTv;
        private TextView contentTv;
        private ImageView avatarIv;
        private RecyclerView picturesRecyclerView;
        private TextView timeTv;
        private Button toCommentsPanelBtn;
        private View dividerView;

        public MyHolder(View itemView) {
            super(itemView);
            usernameTv = (TextView) itemView.findViewById(R.id.tv_username);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            avatarIv = (ImageView) itemView.findViewById(R.id.iv_avatar);
            picturesRecyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerview_pictures);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,2);
            picturesRecyclerView.setLayoutManager(gridLayoutManager);
            timeTv = (TextView) itemView.findViewById(R.id.tv_time);
            toCommentsPanelBtn = (Button) itemView.findViewById(R.id.btn_to_comments_panel);
            dividerView = itemView.findViewById(R.id.divider);
        }
    }
}
